package com.yd.tool;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsoupAutoC168UrlDB {
	public static void main(String[] args) throws MalformedURLException,
			IOException {

		SimpleDateFormat df_ks = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String kaishiTime = df_ks.format(new Date());
		System.out.println("开始----------------" + kaishiTime);

		
		
		
		File tt = new File("/Users/shangruihua/Downloads/tt_auto.txt");
		
		
		String urlruls = "http://auto.16888.com/";
		Document doc = Jsoup.parse(new URL(urlruls).openStream(), "utf-8", urlruls);
		
		
		Elements times_content = doc.getElementsByClass("brand_box");
//		System.out.println(times_content.toString().replace(Jsoup.parse("&nbsp;").text(),""));
		int zong=0;
		for (int i = 0; i < times_content.size(); i++) {
			Element che=times_content.get(i);
			
			
			
			Element chexing_html = che.select("a").first();
			Element chebiao_html = che.select("div div").first().selectFirst("a");
			Elements chexing_all_html = che.select("ul li span.name").select("a");
			
			System.out.println("品牌:"+chexing_html.select("a").attr("name"));
			System.out.println("品牌图片:"+chebiao_html.select("img").attr("src"));
			System.out.println("车型:"+chexing_all_html.text());
			zong+=chexing_all_html.size();
			
			
			
			
			
			
			
			//intxt(tt,chexing_html.toString()+chebiao_html.toString()+chexing_all_html.toString());
			System.out.println();
			System.out.println();
			System.out.println();
			System.out.println();
		}
		System.out.println(zong+"============");

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println("----end----");

		SimpleDateFormat df_js = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String jieshuTime = df_js.format(new Date());
		System.out.println("结束--------" + kaishiTime + "--------" + jieshuTime);

	}

	// 写入txt 追加
	public static void intxt(File file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
